$:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
require "test/unit"
require "html_result"

class HtmlResultTest < Test::Unit::TestCase

    def test_append_pass
        file_name = HtmlResult.new("sample_result").append("test_1")
        doc = open(file_name){|f| Hpricot(f)}
        assert_equal("test_1", doc.at("//tbody/tr/td").inner_html)
        assert_equal(true, File.exist?(file_name))
        File.delete(file_name)
    end

    def test_append_fail
        file_name = HtmlResult.new("sample_result").append("test_2", FakeFailure.new)
        doc = open(file_name){|f| Hpricot(f)}
        assert_equal("test_2", doc.at("//tbody/tr/td").inner_html)
        assert_equal("sample long display failure", doc.at("//tbody/tr/td[2]").inner_html)
        assert_equal(true, File.exist?(file_name))
        File.delete(file_name)
    end

    def test_count_update
        file_name = HtmlResult.new("sample_result").count_update(FakeResult.new)
        doc = open(file_name){|f| Hpricot(f)}
        assert_equal("5", doc.at("//td[@name='test_count']").inner_html)
        assert_equal("10", doc.at("//td[@name='assertion_count']").inner_html)
        assert_equal("1", doc.at("//td[@name='failure_count']").inner_html)
        assert_equal("1", doc.at("//td[@name='error_count']").inner_html)
        assert_equal(true, File.exist?(file_name))
        File.delete(file_name)
    end

    def test_append_pass_fail_count
        doc = HtmlResult.new("sample_result")
        doc.append("test_3")
        doc.append("test_4", FakeFailure.new)
        file_name = doc.count_update(FakeResult.new)
        html = open(file_name){|f| Hpricot(f)}
        assert_equal("test_3", html.at("//tbody/tr/td").inner_html)
        assert_equal("test_4", html.at("//tbody/tr[2]/td").inner_html)
        assert_equal("sample long display failure", html.at("//tbody/tr[2]/td[2]").inner_html)
        assert_equal("5", html.at("//td[@name='test_count']").inner_html)
        assert_equal("10", html.at("//td[@name='assertion_count']").inner_html)
        assert_equal("1", html.at("//td[@name='failure_count']").inner_html)
        assert_equal("1", html.at("//td[@name='error_count']").inner_html)
        assert_equal(true, File.exist?(file_name))
        File.delete(file_name)
    end
end

class FakeResult
    attr_reader :run_count, :assertion_count, :failure_count, :error_count

    def initialize
        @run_count = 5
        @assertion_count = 10
        @failure_count = 1
        @error_count = 1
    end
end

class FakeFailure
    attr_reader :long_display

    def initialize
        @long_display = "sample long display failure"
    end

end